// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-06 09:13:17
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:14:03
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Property/PCONVParser.cpp
 */

#include "PCONVParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPCONVService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PCONV);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/PCONV/PCONV.md>
*/
// 以下需要修改
enum PCONVkey : std::uint8_t
{
    PCONVKEY_PCONID = 2,   // 对流属性识别编号
    PCONVKEY_MID    = 3,   // 材料属性识别编号
    PCONVKEY_FORM   = 4,   // 自由对流所用公式的类型
    PCONVKEY_EXPF   = 5,   // 在所选特定形式的背景下实现的自由对流指数
    PCONVKEY_FTYPE  = 6,   // 自由对流各种配置的公式类型
    PCONVKEY_TID    = 7,   // 指定自由对流换热系数的二维表格函数的TABLEHT条目识别编号
    PCONVKEY_CHLEN  = 12,  // 特征长度
    PCONVKEY_GIDIN  = 13,  // 引用的进口点的网格ID
    PCONVKEY_CE     = 14,  // 定义边界层流动方向的坐标系。
    PCONVKEY_E1     = 15,  // 用于定义坐标系CE中边界层流动方向的向量分量
    PCONVKEY_E2     = 16,  // 用于定义坐标系CE中边界层流动方向的向量分量
    PCONVKEY_E3     = 17   // 用于定义坐标系CE中边界层流动方向的向量分量
};

enum PCONVHIndex : std::uint8_t
{
    PCONVHIndex_H1 = 6,
    PCONVHIndex_H2 = 7,
    PCONVHIndex_H3 = 8,
    PCONVHIndex_H4 = 9,
    PCONVHIndex_H5 = 12,
    PCONVHIndex_H6 = 13,
    PCONVHIndex_H7 = 14,
    PCONVHIndex_H8 = 15,

};

SG::DataStructure::Common::Status BDF::BULKDATA::ParsePCONV (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    // 初始化临时的PCONV数据结构，用于存储解析后的数据
    SG::DataStructure::FEM::PCONVData tempPCONV;

    // TODO(bug 001) m_domainId的正确填写，目前默认设置为1
    tempPCONV.m_domainId = 1;

    // 分割输入字符串EntryInfo，并将其结果存储到dividedEntryInfo

    int curIDKey = 1;  // 当前处理的字段索引

    // TID为浮点数说明采用第二种格式H1-H8
    bool isAlternateFormat = covertToScientificNotation (dividedEntryInfo[PCONVKEY_TID - 1]);

    // 遍历分割后的字符串条目
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空（只包含空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 跳过空字段，继续下一个字段
            continue;
        }
        // 将字符串转换为科学计数法

        switch (curIDKey)
        {
        case PCONVKEY_PCONID:
            tempPCONV.m_id = convertToI64 (curStrKey);
            break;
        case PCONVKEY_MID:
            tempPCONV.m_MID = convertToI64 (curStrKey);
            break;
        case PCONVKEY_FORM:
            tempPCONV.m_FORM = convertToI64 (curStrKey);
            break;
        case PCONVKEY_EXPF:
            tempPCONV.m_EXPF = convertToReal (curStrKey);
            break;
        case PCONVKEY_FTYPE:
            tempPCONV.m_FTYPE = convertToI64 (curStrKey);
            break;
        case PCONVKEY_TID:
            if (!isAlternateFormat)
            {
                tempPCONV.m_TID = convertToI64 (curStrKey);
            }
            break;
        case PCONVKEY_CHLEN:
            if (!isAlternateFormat)
            {
                tempPCONV.m_CHLEN = convertToReal (curStrKey);
            }
            break;
        case PCONVKEY_GIDIN:
            if (!isAlternateFormat)
            {
                tempPCONV.m_GIDIN = convertToI64 (curStrKey);
            }
            break;
        case PCONVKEY_CE:
            if (!isAlternateFormat)
            {
                tempPCONV.m_CE = convertToI64 (curStrKey);
            }
            break;
        case PCONVKEY_E1:
            if (!isAlternateFormat)
            {
                tempPCONV.m_E[0] = convertToReal (curStrKey);
            }
            break;
        case PCONVKEY_E2:
            if (!isAlternateFormat)
            {
                tempPCONV.m_E[1] = convertToReal (curStrKey);
            }
            break;
        case PCONVKEY_E3:
            if (!isAlternateFormat)
            {
                tempPCONV.m_E[2] = convertToReal (curStrKey);
            }
            break;
        default:
            break;
        }

        curIDKey++;  // 处理完当前字段，移动到下一个字段
    }

    // 处理H1-H8
    if (isAlternateFormat)
    {
        for (int i = 6; i <= 15; i++)
        {
            bool const isEmpty = is_all_space (dividedEntryInfo[i]);
            if (isEmpty)
            {
                curIDKey++;  // 跳过空字段，继续下一个字段
                continue;
            }
            switch (i)
            {
            case PCONVHIndex_H1:
                tempPCONV.m_HCF1 = convertToReal (dividedEntryInfo[i]);
                break;
            case PCONVHIndex_H2:
                tempPCONV.m_HCF2 = convertToReal (dividedEntryInfo[i]);

                break;
            case PCONVHIndex_H3:
                tempPCONV.m_HCF3 = convertToReal (dividedEntryInfo[i]);

                break;
            case PCONVHIndex_H4:
                tempPCONV.m_HCF4 = convertToReal (dividedEntryInfo[i]);

                break;
            case PCONVHIndex_H5:
                tempPCONV.m_HCF5 = convertToReal (dividedEntryInfo[i]);

                break;
            case PCONVHIndex_H6:
                tempPCONV.m_HCF6 = convertToReal (dividedEntryInfo[i]);

                break;
            case PCONVHIndex_H7:
                tempPCONV.m_HCF7 = convertToReal (dividedEntryInfo[i]);

                break;
            case PCONVHIndex_H8:
                tempPCONV.m_HCF8 = convertToReal (dividedEntryInfo[i]);

                break;
            default:
                // Handle unknown indices
                break;
            }
        }
    }

    // 将解析后的PCONV数据添加到数据库服务中
    return dbServiceFactory->get<IPCONVService> ()->Add (tempPCONV);
}